package com.example.wacoffee_user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.wacoffee_user.pojo.dto.couponDto;
import com.example.wacoffee_user.pojo.entity.coupon;
import com.example.wacoffee_user.service.ICouponService;
import com.example.wacoffee_user.utils.CurrentHoler;
import com.example.wacoffee_user.utils.Result;
import net.bytebuddy.asm.MemberSubstitution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/coupon")
public class couponController {

    @Autowired
    private ICouponService couponService;
    @GetMapping
    public Result getAllCoupon() {
        List<couponDto> couponDtos=couponService.getAllCoupon(LocalDateTime.now(),CurrentHoler.getCurrentId());
        return Result.success(couponDtos);
    }

    @PostMapping("/{id}")
        public Result gainCoupon(@PathVariable Integer id){
        coupon coupon = couponService.getById(id);
        if(coupon==null)throw new RuntimeException("该优惠券不存在");
        if(coupon.getStatus()==0)throw new RuntimeException("该优惠卷被禁用了");
        if(coupon.getStartTime().isAfter(LocalDateTime.now()))throw new RuntimeException("特惠还未开始");
        if(coupon.getEndTime().isBefore(LocalDateTime.now()))throw new RuntimeException("特惠已结束");
        if(coupon.getStock()<=0)throw new RuntimeException("该优惠卷卖完了！");
        couponService.addCouponToUser(id);
        return Result.success();
    }


}
